package com.gmail.blackdog1987.carcare.adapter;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.adapter.CleanShopAdapter.LatLonComparator;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshop;
import com.gmail.blackdog1987.carcare.entity.TCleanshop;

public class CarChangeAdapter extends BaseAdapter {

	List<TCarchangeshop> datas;
	Context mContext;
	ImageLoader imageLoader;
	boolean distance = false;
	public void setDistance(boolean distance){
		this.distance = distance;
	}
	public CarChangeAdapter(Context context, List<TCarchangeshop> datas) {
		this.datas = datas;
		this.mContext = context;
		imageLoader = ImageLoaderFactory.create(mContext);
	};
	
	public void addDatas(List<TCarchangeshop> datas){
		this.datas.addAll(datas);
		if(distance){
			Collections.sort(this.datas, new LatLonComparator());
		}
	}
	
	public void setDatas(List<TCarchangeshop> datas){
		if(this.datas==null){
			this.datas = new ArrayList<TCarchangeshop>();
		}
		this.datas.clear();
		this.datas.addAll(datas);
		if(distance){
			Collections.sort(this.datas, new LatLonComparator());
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return datas==null?0:datas.size();
	}

	@Override
	public TCarchangeshop getItem(int position) {
		return datas==null?null:datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final TCarchangeshop m = getItem(position);
//		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_carchange, null);
			holder = new ViewHolder();
			holder.mHead = (CubeImageView) convertView.findViewById(R.id.carchange_img);
			holder.mTitle = (TextView) convertView.findViewById(R.id.carchange_title);
			holder.mExt = (TextView) convertView.findViewById(R.id.carchange_ext);
//			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		holder.mTitle.setText(m.getTitle());
		holder.mExt.setText(m.getLocation());
		
		// set views data
		
		if(m.headurl!=null && m.headurl.length()!=0){
			App.loadImg(holder.mHead, imageLoader, m.headurl);
		}
//		convertView.setTag(holder);
		return convertView;
	}

	public class ViewHolder {
		TextView mTitle;
		TextView mExt;
		CubeImageView mHead;
	}
	
	
	public class LatLonComparator implements Comparator<TCarchangeshop> {

		@Override
		public int compare(TCarchangeshop lhs, TCarchangeshop rhs) {
			if (App.location == null) {
				return 0;
			}
			if (lhs == null) {
				return -1;
			}
			if (rhs == null) {
				return 1;
			}
			double m = DistanceUtil.getDistance(new LatLng(App.location.getLatitude(), App.location.getLongitude()),
					new LatLng(lhs.getLat() == null ? 0 : lhs.getLat(), lhs.getLon() == null ? 0 : lhs.getLon()))
					- DistanceUtil.getDistance(new LatLng(App.location.getLatitude(), App.location.getLongitude()),
							new LatLng(rhs.getLat() == null ? 0 : rhs.getLat(), rhs.getLon() == null ? 0 : rhs.getLon()));
			if (m > 0) {
				return 1;
			} else if (m == 0) {
				return 0;
			} else {
				return -1;
			}
		}

	}

}
